package com.xcm.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.google.common.collect.Lists;
import com.xcm.constant.BusinessConstant;
import com.xcm.constant.DateFormatConstant;
import com.xcm.constant.OperationLogConstant;
import com.xcm.constant.PageConstant;
import com.xcm.dao.DeviceUpgradeMapper;
import com.xcm.model.TbDeviceUpgrade;
import com.xcm.model.TbOperationLog;
import com.xcm.model.dto.DeviceUpgradeListDTO;
import com.xcm.model.vo.DeviceUpgradeListVO;
import com.xcm.page.PageInfo;
import com.xcm.service.DeviceUpgradeService;
import com.xcm.service.OperationLogService;
import com.xcm.util.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.rmi.ServerException;
import java.util.List;

/**
 * 终端升级Service实现类
 */
@Service(value = "deviceUpgradeService")
public class DeviceUpgradeServiceImpl implements DeviceUpgradeService {
	@Autowired
	private DeviceUpgradeMapper deviceUpgradeMapper;
	@Autowired
	private OperationLogService operationLogService;

	/**
	 * 通过对应机型获取终端升级
	 * getDeviceUpgradeByModel
	 *
	 * @param model 对应机型
	 * @return TbDeviceUpgrade
	 */
	@Override
	public TbDeviceUpgrade getDeviceUpgradeByModel(String model) {
		return deviceUpgradeMapper.getDeviceUpgradeByModel(model);
	}

	/**
	 * 通过mac取终端升级
	 * getDeviceUpgradeByMac
	 *
	 * @param mac mac地址
	 * @return TbDeviceUpgrade
	 */
	@Override
	public TbDeviceUpgrade getDeviceUpgradeByMac(String mac) {
		return deviceUpgradeMapper.getDeviceUpgradeByMac(mac);
	}

	/**
	 * 保存终端升级
	 *
	 * @param deviceUpgrade TbDeviceUpgrade对象
	 */
	@Override
	public void saveDeviceUpgrade(TbDeviceUpgrade deviceUpgrade) {
		deviceUpgrade.setUpdateTime(System.currentTimeMillis());
		deviceUpgrade.setActive(BusinessConstant.DEVICE_UPGRADE_FORCE_NO);
		deviceUpgrade.setUpgradeType(BusinessConstant.DEVICE_UPGRADE_TYPE_SOFTWARE);
		deviceUpgrade.setIsdelete(BusinessConstant.DEVICE_UPGRADE_IS_DELETED_NO);
		// TODO: 2018/5/4  //展示为空，用户配置在公共登陆系统 deviceUpgrade.setUploader("用户名");
		deviceUpgradeMapper.saveDeviceUpgrade(deviceUpgrade);
		//保存操作日志
		TbOperationLog operationLog = new TbOperationLog();
		operationLog.setOperationTypeId(OperationLogConstant.OPERATION_LOG_TYPE_DEVICE);
		operationLog.setOperationContent("上传Apk升级文件:" + deviceUpgrade.getName());
		operationLog.setIsSuccess(OperationLogConstant.OPERATION_LOG_SUCCESS);
		operationLog.setOperationTime(System.currentTimeMillis());
		// TODO: 2018/4/9 处理用户ID和IP地址
		//展示设置操作用户名、用户真实姓名、IP为空，用户保存在统一登录系统
		operationLog.setUserName("");
		operationLog.setRealName("");
		operationLog.setOperationIp("");
		operationLogService.saveOperationLog(operationLog);
	}

	/**
	 * 删除终端升级
	 *
	 * @param deviceUpgradeIds 终端ID字符串集合(如："1,2,3,4,5")
	 */
	@Override
	public void deleteDeviceUpgradeBatch(String deviceUpgradeIds) {
		String[] deviceUpgradeIdArray = deviceUpgradeIds.split(",");
		deviceUpgradeMapper.updateIsdeleteBatch(deviceUpgradeIdArray, BusinessConstant.DEVICE_UPGRADE_IS_DELETED_YES);
		//保存操作日志
		TbOperationLog operationLog = new TbOperationLog();
		operationLog.setOperationTypeId(OperationLogConstant.OPERATION_LOG_TYPE_DEVICE);
		operationLog.setOperationContent("删除Apk升级文件,id:" + deviceUpgradeIds);
		operationLog.setIsSuccess(OperationLogConstant.OPERATION_LOG_SUCCESS);
		operationLog.setOperationTime(System.currentTimeMillis());
		// TODO: 2018/4/9 处理用户ID和IP地址
		//展示设置操作用户名、用户真实姓名、IP为空，用户保存在统一登录系统
		operationLog.setUserName("");
		operationLog.setRealName("");
		operationLog.setOperationIp("");
		operationLogService.saveOperationLog(operationLog);
	}

	/**
	 * 更新强制升级
	 *
	 * @param deviceUpgradeId 终端升级ID
	 * @param force           是否强制       (1强制升级;0不强制)
	 */
	@Override
	public void updateActive(Long deviceUpgradeId, Integer force) {
		long updateTime = System.currentTimeMillis();
		deviceUpgradeMapper.updateActive(deviceUpgradeId, force, updateTime);
		//保存操作日志
		String logContent = "";
		if (BusinessConstant.DEVICE_UPGRADE_FORCE_YEE == force) {
			logContent = "设置Apk强制升级,id:" + deviceUpgradeId;
		} else {
			logContent = "取消Apk强制升级,id:" + deviceUpgradeId;
		}
		TbOperationLog operationLog = new TbOperationLog();
		operationLog.setOperationTypeId(OperationLogConstant.OPERATION_LOG_TYPE_DEVICE);
		operationLog.setOperationContent(logContent);
		operationLog.setIsSuccess(OperationLogConstant.OPERATION_LOG_SUCCESS);
		operationLog.setOperationTime(System.currentTimeMillis());
		// TODO: 2018/4/9 处理用户ID和IP地址
		//展示设置操作用户名、用户真实姓名、IP为空，用户保存在统一登录系统
		operationLog.setUserName("");
		operationLog.setRealName("");
		operationLog.setOperationIp("");
		operationLogService.saveOperationLog(operationLog);
	}

	/**
	 * 获取终端升级分页列表
	 *
	 * @param model    对应机型
	 * @param pageNum  当前页
	 * @param pageSize 每页显示条数
	 * @return Page<TbDeviceUpgrade>
	 */
	@Override
	public PageInfo<DeviceUpgradeListVO> listDeviceUpgradeByPage(String model, Integer pageNum, Integer pageSize) {
		pageNum = pageNum == null ? PageConstant.DEFAULT_PAGE_NUM : pageNum;
		pageSize = pageSize == null ? PageConstant.DEFAULT_PAGE_SIZE : pageSize;
		PageHelper.startPage(pageNum, pageSize);
		Page<DeviceUpgradeListDTO> page = deviceUpgradeMapper.listDeviceUpgradeByPage(BusinessConstant.DEVICE_UPGRADE_IS_DELETED_NO, model);
		if (page == null) {
			return new PageInfo<>(pageNum, pageSize, 0, Lists.newArrayList());
		}
		//把Page转化为PageInfo
		PageInfo<DeviceUpgradeListVO> pageInfo = new PageInfo<>();
		pageInfo.setPageNum(pageNum);
		pageInfo.setPageSize(pageSize);
		pageInfo.setTotal(page.getTotal());
		//把DTO转化为VO,并把时间戳转化为字符串
		List<DeviceUpgradeListDTO> deviceUpgradeDtoList = page.getResult();
		List<DeviceUpgradeListVO> deviceUpgradeVoList = Lists.newArrayList();
		for (DeviceUpgradeListDTO dto : deviceUpgradeDtoList) {
			DeviceUpgradeListVO deviceUpgradeVo = new DeviceUpgradeListVO();
			deviceUpgradeVo.setDeviceUpgradeId(dto.getDeviceUpgradeId());
			deviceUpgradeVo.setActive(dto.getActive());
			deviceUpgradeVo.setModel(dto.getModel());
			deviceUpgradeVo.setSize(dto.getSize());
			deviceUpgradeVo.setVersionCode(dto.getVersionCode());
			deviceUpgradeVo.setName(dto.getName());
			Long updateTime = dto.getUpdateTime();
			if (updateTime == null) {
				deviceUpgradeVo.setUpdateTime("");
			} else {
				deviceUpgradeVo.setUpdateTime(DateUtils.longTimeStampToStringDate(updateTime, DateFormatConstant.LONG_DATE_FORMAT_STR));
			}
			deviceUpgradeVo.setSoftwareVersionDescription(dto.getSoftwareVersionDescription());
			deviceUpgradeVo.setUrl(dto.getUrl());
			deviceUpgradeVoList.add(deviceUpgradeVo);
		}
		pageInfo.setList(deviceUpgradeVoList);
		return pageInfo;
	}

	/**
	 * 获取所有版本号
	 *
	 * @return List<String>
	 */
	@Override
	public List<String> listAllVersionCode() {
		return deviceUpgradeMapper.listAllVersionCode();
	}

	/**
	 * 通过ID获取终端升级
	 *
	 * @param deviceUpgradeId 终端升级ID
	 * @return TbDeviceUpgrade
	 */
	@Override
	public TbDeviceUpgrade getDeviceUpgradeById(Long deviceUpgradeId) throws ServerException {
		if (deviceUpgradeId == null) {
			throw new ServerException("终端升级ID不能为空");
		}
		return deviceUpgradeMapper.getDeviceUpgradeById(deviceUpgradeId);
	}

	/**
	 * 更新终端升级
	 *
	 * @param deviceUpgrade TbDeviceUpgrade
	 */
	@Override
	@Transactional(readOnly = false, rollbackFor = Exception.class)
	public void updateDeviceUpgrade(TbDeviceUpgrade deviceUpgrade) throws ServerException {
		TbDeviceUpgrade oldDeviceUpgrade = getDeviceUpgradeById(deviceUpgrade.getDeviceUpgradeId());
		oldDeviceUpgrade.setUrl(deviceUpgrade.getUrl());
		oldDeviceUpgrade.setName(deviceUpgrade.getName());
		oldDeviceUpgrade.setVersionName(deviceUpgrade.getVersionName());
		oldDeviceUpgrade.setSize(deviceUpgrade.getSize());
		oldDeviceUpgrade.setModel(deviceUpgrade.getModel());
		oldDeviceUpgrade.setSoftwareVersionDescription(deviceUpgrade.getSoftwareVersionDescription());
		oldDeviceUpgrade.setVersionCode(deviceUpgrade.getVersionCode());
		oldDeviceUpgrade.setUpdateTime(System.currentTimeMillis());
		deviceUpgradeMapper.updateDeviceUpgrade(oldDeviceUpgrade);
	}
}
